import 'package:get/get.dart';
import 'package:getx_scaffold/common/index.dart';
import 'package:oramemo/core/repositories/default_api.dart';
import 'package:oramemo/entity/base_entity.dart';
import 'package:oramemo/entity/device_mine_entity.dart';
import 'package:oramemo/entity/requst/device_put_entity.dart';
import 'package:oramemo/pages/device/relation_select_page.dart';

class DeviceDetailLogic extends GetxController {
  DeviceMineData? device = Get.arguments;

  final deviceName = '时光机'.obs;

  @override
  void onInit() {
    deviceName.value = device?.device?.deviceName ?? '';
    super.onInit();
  }

  // 更新设备名称
  void updateDevice({
    String? name,
    String? customModelId,
  }) async {
    if(name!=null) {
      deviceName.value = name;
    }
    DevicePutEntity entity = DevicePutEntity()
      ..id = device?.device?.id
      ..customModelId = customModelId;
    final res = await DefaultApi().oraDevicePut<BaseEntity>(entity: entity);
    if (res?.isSuccess == true) {
      Toast.show('修改成功');
    }
    update();
  }

  // 解绑设备
  Future<void> unbindDevice() async {
    // 实现解绑逻辑
    try {
      // API调用
      Get.back();
    } catch (e) {
      Get.snackbar('错误', '解绑失败：$e');
    }
  }
}
